const app = getApp()
let _this;
import {urls} from '../../utils/urls'
Page({
  data: {
    school_list:[],
    load:false,
    search : '',
    search_list :[]
  },
  onLoad: function () {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#6e42d3',
    })
    _this = this
    this.getArea()
  },
  selectSchool(e){
    let index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '请确认您的选择',
      content: '您的选择是"' + _this.data.school_list[index].name+'"',
      cancelText:'我点错了',
      confirmText:'确认',
      confirmColor:'#6887e1',
      success(res){
        if(res.confirm){
          wx.setStorageSync('school', _this.data.school_list[index])
          _this.getService(_this.data.school_list[index].id)
        }else{}
      }
    })   
  },
  getArea(){
    this.setData({
      load:true
    })
    app.http('GET', urls.school, 
    function(res){
      if(res.code == 100){
        _this.setData({
          school_list: res.results,
          load:false
        })        
      }else{
        wx.showToast({
          title: '请求失败',
          icon:'none'
        })
      }     
    })
  },
  getService(){
    wx.setStorageSync("service", wx.getStorageSync('school').service)
    wx.navigateBack({
      delta: 1
    })
  }, 
  searchInput(e){
    let search = e.detail.value
    _this.setData({
      search : search
    })
    let school_list= _this.data.school_list
    let search_list = []
    for(let i  in school_list){
      if(school_list[i].name.indexOf(search)>=0){
        search_list.push(school_list[i])
      }
    }
    _this.setData({
      search_list : search_list
    })
  },
  onShow(){
    _this.getArea()
  }
})